home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / ASSEMBLE / H235A.ZIP / ASM_0_M.ZIP / FIA.ASM < prev    next >
Assembly Source File  |  1988-12-08  |  4KB  |  156 lines

  1. page 66, 132
  2. ;   Narrow on printer
  3. page
  4.  
  5. ;---- fio.asm -------------------------------------------------------------
  6. ; Buffered I/O for lazy programmers.
  7. ; Expects user to set these provided variables:
  8. ;      f_ihand, f_ohand: handles of input & output files, respectively
  9. ;       f_ibuf, f_obuf  : word pointers to buffer areas;
  10. ;      f_ilen, f_olen  : word lengths of buffer areas;
  11. ; Provides:
  12. ;       f_ibufend, f_obufend: word ptrs to current ends of buffer areas;
  13. ;       f_imore, f_omore    : routines to flush input & output buffers;
  14. ;       f_getc, f_putc      : Get/put al; flags same as imore & omore.
  15. ;      f_io_init           : Given f_ilen & f_olen, calls new & sets up.
  16. ;
  17. ; All these routines reserve SI and DI for their io pointers.
  18. ; Getc and imore detect physical EOF; you'll have to compare with
  19. ; ^Z yourself if you need logical EOF.
  20. ;
  21. ; Version of 23 Aug 1984; DRK.
  22.     extrn   new: near;
  23.  
  24.        public  f_ihand, f_ohand, f_ibuf, f_obuf, f_ilen, f_olen;
  25.        public  f_ibufend, f_obufend, f_imore, f_omore;
  26.        public  f_getc, f_putc;
  27.        public  f_io_init
  28.  
  29.  
  30. dos    macro   fn
  31.        mov     ah, fn
  32.        int     21h
  33.        endm
  34.  
  35. code   segment para public 'CODE'
  36. assume cs:code,ds:code
  37.  
  38. f_ihand                dw      ?
  39. f_ohand                dw      ?
  40.  
  41. f_ibuf         dw      ?       ; where input buffer starts
  42. f_ibufend      dw      ?       ; points to just past last byte
  43. f_ilen         dw      ?
  44. f_obuf         dw      ?       ; where output buffer starts
  45. f_obufend      dw      ?
  46. f_olen         dw      ?
  47.  
  48. ;-- f_io_init -----------
  49. f_io_init      proc    near
  50.        mov     cx, f_ilen
  51.        call    new
  52.        mov     f_ibuf, bx
  53.        mov     f_ibufend, bx
  54.        mov     si, bx
  55.  
  56.        mov     cx, f_olen
  57.        call    new
  58.        mov     f_obuf, bx
  59.        mov     di, bx
  60.        add     bx, cx
  61.        mov     f_obufend, bx
  62.  
  63.        ret
  64. f_io_init      endp
  65.  
  66.  
  67. ;-- f_imore -----------------------------------------------
  68. ; Reads from file into f_ibuf.
  69. ; Resets f_ibufend.
  70. ; Loads SI with new f_ibufptr.
  71. ; Returns Z=true if at EOF, CY=true if error reading file.
  72.  
  73. f_imore        proc    near
  74.  
  75.        mov     bx, f_ihand
  76.        mov     cx, f_ilen
  77.        mov     dx, f_ibuf
  78.        DOS     3fh             ; read from file
  79.        jc      rerr
  80.  
  81.        mov     si, dx
  82.        add     dx, ax
  83.        mov     f_ibufend, dx
  84.  
  85.        or      ax, ax          ; check physical EOF
  86.        clc
  87.  
  88. rerr:  ret
  89.  
  90. f_imore        endp
  91.  
  92. ;-----------------------------------
  93.  
  94. ;-- f_omore ---------------------------------------
  95. ; Expects DI to have f_obufptr.
  96. ; Writes f_obuffer to output file.
  97. ; Resets f_obufend
  98. ; Loads DI with new f_obufptr.
  99.  
  100. f_omore        proc    near
  101.  
  102.        mov     dx, f_obuf
  103.        mov     bx, f_ohand
  104.        mov     cx, di
  105.        sub     cx, dx
  106.        
  107.        DOS     40H             ; write to file
  108.        jc      werr
  109.        sub     cx, ax
  110.        jz      afinished
  111.        dec     cx
  112.        mov     ax, -1
  113.        jnz     werr            ; account for WIERDNESS in DOS:
  114.                                ; when writing to device, the ^Z is not
  115.                                ; transferred.  Ignore error if all but one
  116.                                ; char was written.
  117. afinished:
  118.        mov     di, dx
  119.        clc
  120.  
  121. wdone: ret
  122.  
  123. werr:  stc
  124.        jmp     wdone
  125.  
  126. f_omore        endp
  127.  
  128.  
  129. ;-----------------------------------
  130. f_getc proc    near
  131.        cmp     si, f_ibufend
  132.        jnz     f_gcok
  133.                call    f_imore
  134. f_gcok:        lodsb
  135.        ret
  136. f_getc endp
  137.  
  138.  
  139. f_putc proc    near
  140.        cmp     di, f_obufend
  141.        jnz     pok
  142.                push    ax
  143.                call    f_omore
  144.                pop     ax
  145. pok:   stosb
  146.        ret
  147. f_putc endp
  148.  
  149.  
  150.  
  151. code   ends
  152.  
  153.        end
  154.  
  155.  
  156.